package com.suyin.ximoon.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyin.ximoon.annotation.SystemLog;
import com.suyin.ximoon.controller.base.BaseController;
import com.suyin.ximoon.model.LyRole;
import com.suyin.ximoon.model.bo.Page;
import com.suyin.ximoon.plugin.mybatis.Criteria;
import com.suyin.ximoon.plugin.mybatis.Restriction;
import com.suyin.ximoon.service.LyRoleService;
import com.suyin.ximoon.util.Common;
import com.suyin.ximoon.util.Tools;

/**
 * 
 * @author lanyuan 2014-11-19
 * @Email: mmm333zzz520@163.com
 * @version 3.0v
 */
@Controller
@RequestMapping("/role/")
public class RoleController extends BaseController {
	@Resource
	private LyRoleService lyroleService;

	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/system/role/list";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public Page findByPage(String pageNow,String pageSize,String name) throws Exception {
		 Page page = new Page();
         if (Tools.notEmpty( pageNow))
         {
             page.setCurrentPage(Integer.parseInt(pageNow));
         }
         if (Tools.notEmpty(pageSize))
         {
             page.setShowCount(Integer.parseInt(pageSize));
         }
         else
         {
             page.setShowCount(20);
         }
         LyRole lyrole = new LyRole();
         if (Tools.notEmpty(name))
         {
        	 lyrole.setName(name);
         }
         lyrole.setPage(page);
         page.setRecords(lyroleService.queryByPage(lyrole));
		return page ;
	}

	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/system/role/add";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="组管理-新增组")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(LyRole roleFormMap) throws Exception {
		lyroleService.insert(roleFormMap);
		return "success";
	}

	@ResponseBody
	@RequestMapping("deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="组管理-删除组")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		for (String id : ids) {
			Criteria c = new Criteria(); 
			c .add(Restriction.eq("id", id));
			lyroleService.delete(c);
		}
		return "success";
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			model.addAttribute("role", lyroleService.queryOne(Integer.valueOf(id)));
		}
		return Common.BACKGROUND_PATH + "/system/role/edit";
	}

	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="组管理-修改组")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(LyRole roleFormMap) throws Exception {
		lyroleService.update(roleFormMap);
		return "success";
	}
	
	
	@RequestMapping("selRole")
	public String seletRole(Model model,String userId) throws Exception {
		 
		Map<String, Object> map = new HashMap<String, Object>();
		String ugid = "";
		if(null!=userId){
			map.put("userId", userId);
			List<LyRole> list = lyroleService.queryUserRole(map);
			
			for (LyRole ml : list) {
				ugid += ml.getId()+",";
			}
			ugid = Common.trimComma(ugid);
			model.addAttribute("txtRoleSelect", ugid);
			model.addAttribute("userRole", list);
			
		}
		Criteria c = new Criteria(); 
		//c.add(Restriction.in("id", StringUtil.StrList(ugid)));
		List<LyRole> roles = lyroleService.query(c);
		model.addAttribute("role", roles);
		return Common.BACKGROUND_PATH + "/system/user/roleSelect";
	}

}